package com.citycloud.datac.client.configuration;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.util.CollectionUtils;

import com.citycloud.datac.client.cache.CacheManager;
import com.citycloud.datac.client.client.DataCOauth2Client;
import com.citycloud.datac.client.dto.ResourceServerConfigurer;
import com.citycloud.datac.client.filter.Oauth2AuthorizationFilter;
import com.citycloud.datac.client.properties.SecurityConfiguration;
import com.citycloud.datac.client.properties.SecurityConfigurter;
import com.citycloud.datac.client.token.ResourceServerTokenServices;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
@Import(value = SecurityConfiguration.class)
public class DataCOAuth2ClientConfiguration {
	final SecurityConfigurter.SecurityClientConfig clientConfig;
	final SecurityConfigurter.SecurityResourceConfig resourceConfig;
	final SecurityConfigurter.SecurityFilter filter;
	@Autowired(required = false)
	private CacheManager cacheManager;
	@Autowired(required = false)
	private ResourceServerTokenServices tokenServices;

	private List<DataCOAuth2ClientConfigurer> configures;

	public DataCOAuth2ClientConfiguration(final SecurityConfigurter.SecurityClientConfig clientConfig,
			final SecurityConfigurter.SecurityResourceConfig resourceConfig,
			final SecurityConfigurter.SecurityFilter filter) {
		this.clientConfig = clientConfig;
		this.resourceConfig = resourceConfig;
		this.filter = filter;
	}

	@Autowired(required = false)
	public void setConfigurers(List<DataCOAuth2ClientConfigurer> configures) {
		this.configures = configures;
	}

	protected DataCOauth2Client configure() {
		DataCOAuth2ClientConfigurerAdapter adapter = new DataCOAuth2ClientConfigurerAdapter(clientConfig,
				resourceConfig);
		ResourceServerConfigurer resourceServerConfigurer = new ResourceServerConfigurer();
		if (cacheManager != null) {
			resourceServerConfigurer.cacheManager(cacheManager);
		}
		if (tokenServices != null) {
			resourceServerConfigurer.tokenServices(tokenServices);
		}
		if (!CollectionUtils.isEmpty(configures)) {
			for (DataCOAuth2ClientConfigurer configurer : configures) {
				configurer.configure(resourceServerConfigurer);
				return configurer.getOauth2Client();
			}
		} else {
			adapter.configure(resourceServerConfigurer);
			return adapter.getOauth2Client();
		}
		return null;
	}

	/**
	 * 创建Client实例，并完成初始化
	 */
	@Bean(name = "dataCOauth2Client")
	public DataCOauth2Client oauth2Client() {
		return this.configure();
	}

	@Bean(name = "oauth2AuthorizationFilter")
	public Oauth2AuthorizationFilter oauth2AuthorizationFilter() {
		return new Oauth2AuthorizationFilter();
	}

	@Bean
	public FilterRegistrationBean<Oauth2AuthorizationFilter> getfilter() {
		FilterRegistrationBean<Oauth2AuthorizationFilter> registration = new FilterRegistrationBean<Oauth2AuthorizationFilter>();
		registration.setFilter(oauth2AuthorizationFilter());
		registration.setName("authorizationFilter");
		registration.setEnabled(filter.getEnable() == null ? false : filter.getEnable());
		if (filter.getUrlpatterns() != null) {
			String[] filterUrlPatterns = filter.getUrlpatterns().split(",");
			for (int i = 0; i < filterUrlPatterns.length; i++) {
				registration.addUrlPatterns(filterUrlPatterns[i].trim());
				log.info("初始化 QcloudAuthorizationFilter, Set urlPatterns = " + filterUrlPatterns[i]);
			}
		}
		if (StringUtils.isNotBlank(filter.getExcludedUri())) {
            registration.addInitParameter("excludedUri", filter.getExcludedUri());
        }
		log.info("初始化 QcloudAuthorizationFilter, Set excludedUri = " + filter.getExcludedUri());
		return registration;
	}
}
